<?php
require_once '../../library/config.php';
require_once '../library/functions.php';

checkUser();

$action = isset($_GET['action']) ? $_GET['action'] : '';

switch ($action) {
	
	case 'addTopic' :
		addTopic();
		break;
		
	case 'modifyTopic' :
		modifyTopic();
		break;
		
	case 'deleteTopic' :
		deleteTopic();
		break;
	
	case 'deleteImage' :
		deleteImage();
		break;
    

	default :
	    // if action is not defined or unknown
		// move to main Topic page
		header('Location: index.php');
}


function addTopic()
{
    $nn_id   = $_POST['cboTopicGroup'];
    $madt        = $_POST['txtMadt'];
	$tendt		 = $_POST['txtTendt'];
	$tomtat		 = $_POST['txtTomtat'];
	$noidung     = $_POST['txtNoidung'];
	$ngaybatdau	 = $_POST['txtNgaybatdau'];
	$ngayketthuc = $_POST['txtNgayketthuc'];
	$donvi      = $_POST['txtDonvi'];
	$trangthai   = $_POST['txtTrangthai'];
		
	$sql2 	= "select id from donvi where id = $_dv_id";
	
	$result2 = dbQuery($sql2) or die('' . mysql_error());
	
	if (mysql_num_rows($result2)<>0){
		while ($row = mysql_fetch_array($result2))
			$dv_id = $row["id"];
	}

	$sql1   = "INSERT INTO detai (madt, tendt, tomtat_dt, noidung_dt, ngaybatdau, ngayketthuc, dv_id, trangthai)
	          VALUES ('$madt', '$tendt', '$tomtat', '$noidung', '$ngaybatdau', '$ngayketthuc', $dv_id, trangthai)";

	$result1 = dbQuery($sql1);
		
	/*$sql3   = "INSERT INTO tacgia_nhomnc(tg_id, nhomnc_id)
	          VALUES($tg_id, $nhomnc_id)";
	
	$result1 = dbQuery($sql3);*/
	
	header("Location: index.php?nn_id=$nn_id");	
}

/*
	Upload an image and return the uploaded image name 
*/
function uploadTopicImage($inputName, $uploadDir)
{
	$image     = $_FILES[$inputName];
	$imagePath = '';
	$thumbnailPath = '';
	
	// if a file is given
	if (trim($image['tmp_name']) != '') {
		$ext = substr(strrchr($image['name'], "."), 1); //$extensions[$image['type']];

		// generate a random new file name to avoid name conflict
		$imagePath = md5(rand() * time()) . ".$ext";
		
		list($width, $height, $type, $attr) = getimagesize($image['tmp_name']); 

		// make sure the image width does not exceed the
		// maximum allowed width
		if (LIMIT_Topic_WIDTH && $width > MAX_Topic_IMAGE_WIDTH) {
			$result    = createThumbnail($image['tmp_name'], $uploadDir . $imagePath, MAX_Topic_IMAGE_WIDTH);
			$imagePath = $result;
		} else {
			$result = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath);
		}	
		
		if ($result) {
			// create thumbnail
			$thumbnailPath =  md5(rand() * time()) . ".$ext";
			$result = createThumbnail($uploadDir . $imagePath, $uploadDir . $thumbnailPath, THUMBNAIL_WIDTH);
			
			// create thumbnail failed, delete the image
			if (!$result) {
				unlink($uploadDir . $imagePath);
				$imagePath = $thumbnailPath = '';
			} else {
				$thumbnailPath = $result;
			}	
		} else {
			// the Topic cannot be upload / resized
			$imagePath = $thumbnailPath = '';
		}
		
	}

	
	return array('image' => $imagePath, 'thumbnail' => $thumbnailPath);
}

/*
	Modify a Topic
*/
function modifyTopic()
{
	$nhomnc_id 	= (int)$_POST['cboGroup'];
	$matg   	= $_GET['matg'];	
    $hotentg    = $_POST['txtHotentg'];
	$ngaysinh 	= $_POST['txtNgaysinh'];
	$quequantg  = $_POST['txtQuequan'];
	$madv 		= $_POST['txtMadv'];
	$chucvu  	= $_POST['txtChucvu'];
	$hocham 	= $_POST['txtHocham'];
	$hocvi 		= $_POST['txtHocvi'];

	//$images = uploadTopicImage('fleImage', SRV_ROOT . 'images/product/');

/*	$mainImage = $images['image'];
	$thumbnail = $images['thumbnail'];

	// if uploading a new image
	// remove old image
	if ($mainImage != '') {
		_deleteImage($matg);
		
		$mainImage = "'$mainImage'";
		$thumbnail = "'$thumbnail'";
	} else {
		// if we're not updating the image
		// make sure the old path remain the same
		// in the database
		$mainImage = 'pd_image';
		$thumbnail = 'pd_thumbnail';
	}
	*/		
	$sql1   = "UPDATE tacgia 
               SET hotentg = '$hotentg', ngaysinh = '$ngaysinh', quequantg = '$quequantg', madv = '$madv', chucvu = '$chucvu', hocham = '$hocham', hocvi = '$hocvi'
			   WHERE matg = '$matg'" ;		   
	$result = dbQuery($sql1);
	
	$sql3 	= "select id from tacgia where matg = '$matg'";
	
	$result3 = dbQuery($sql3) or die('' . mysql_error());
	
	if (mysql_num_rows($result3)<>0){
		while ($row = mysql_fetch_array($result3))
			$tg_id = $row["id"];
	}
	
	$sql4 	= "select tg_id from tacgia_nhomnc where tg_id = $tg_id";
	
	$result4 = dbQuery($sql4) or die('' . mysql_error());
	
	if (mysql_num_rows($result4)==0){
		$sql2 = "INSERT INTO tacgia_nhomnc(tg_id, nhomnc_id, ngaythamgia) VALUES ($tg_id, $nhomnc_id, now())";
	}else
	$sql2   = "UPDATE tacgia_nhomnc
               SET nhomnc_id = $nhomnc_id
			   WHERE tg_id = $tg_id" ;		   
	$result = dbQuery($sql2);
	header('Location: index.php');			  
}

/*
	Remove a Topic
*/
function deleteTopic()
{/*

	if (isset($_GET['matg']) && (int)$_GET['matg'] !='') {
		$matg = $_GET['matg'];
	} else {
		header('Location: index.php');
	}
	
	// remove any references to this Topic from
	// tbl_order_item and tbl_cart
	$sql = "DELETE FROM tbl_order_item
	        WHERE pd_id = $matg";
	dbQuery($sql);
			
	$sql = "DELETE FROM tbl_cart
	        WHERE pd_id = $matg";	
	dbQuery($sql);
			
	// get the image name and thumbnail
	$sql = "SELECT pd_image, pd_thumbnail
	        FROM tbl_Topic
			WHERE pd_id = $matg";
			
	$result = dbQuery($sql);
	$row    = dbFetchAssoc($result);
	
	// remove the Topic image and thumbnail
	if ($row['pd_image']) {
		unlink(SRV_ROOT . 'images/product/' . $row['pd_image']);
		unlink(SRV_ROOT . 'images/product/' . $row['pd_thumbnail']);
	}
	
	// remove the Topic from database;
	$sql = "DELETE FROM tbl_Topic 
	        WHERE pd_id = $matg";
	dbQuery($sql);
	
	header('Location: index.php?catId=' . $_GET['catId']);*/
}


/*
	Remove a Topic image
*/
function deleteImage()
{
/*
	if (isset($_GET['matg']) && (int)$_GET['matg'] > 0) {
		$matg = (int)$_GET['matg'];
	} else {
		header('Location: index.php');
	}
	
	$deleted = _deleteImage($matg);

	// update the image and thumbnail name in the database
	$sql = "UPDATE tbl_Topic
			SET pd_image = '', pd_thumbnail = ''
			WHERE pd_id = $matg";
	dbQuery($sql);		

	header("Location: index.php?view=modify&matg=$matg");
	*/
}

function _deleteImage($matg)
{
/*
	// we will return the status
	// whether the image deleted successfully
	$deleted = false;
	
	$sql = "SELECT pd_image, pd_thumbnail 
	        FROM tbl_Topic
			WHERE pd_id = $matg";
	$result = dbQuery($sql) or die('Cannot delete Topic image. ' . mysql_error());
	
	if (dbNumRows($result)) {
		$row = dbFetchAssoc($result);
		extract($row);
		
		if ($pd_image && $pd_thumbnail) {
			// remove the image file
			$deleted = @unlink(SRV_ROOT . "images/product/$pd_image");
			$deleted = @unlink(SRV_ROOT . "images/product/$pd_thumbnail");
		}
	}
	
	return $deleted;*/
}




?>